package ar.edu.sovietrussia.customcomponents.loaning;

import java.util.Map;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.user.LoanService;
import users.LibrarianRole;
import users.Person;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainLibrarianComponent;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainUserComponent;
import ar.edu.sovietrussia.customcomponents.searchbook.MainSearchComponent;
import ar.edu.sovietrussia.customcomponents.util.BackToMainWindowGenericComponent;
import ar.edu.sovietrussia.customcomponents.util.IDoReplaces;
import ar.edu.sovietrussia.customcomponents.util.IFeedbacker;
import ar.edu.sovietrussia.customcomponents.util.WrapperPanel;
import dtos.BookInfoDTO;

/**
 * Activated when pressing the little ALQUILAR button y the main search component
 */

@AuthorizeInstantiation({ "USER", "ADMIN" })
public abstract class LoanConfirmationComponent extends BackToMainWindowGenericComponent implements IFeedbacker,
        IDoReplaces {

    private static final long serialVersionUID = -6270909527663689822L;

    @SpringBean
    private LoanService loanService;

    private BookInfoDTO book;

    public LoanConfirmationComponent(final String id) {
        this(id, new BookInfoDTO());
    }

    public LoanConfirmationComponent(final String id, final BookInfoDTO aBook) {
        super(id);
        this.book = aBook;

        final Person aModel = (Person) this.getSession().getAttribute("model");

        Form<String> form = new Form<String>("confirmLoanForm");

        form.add(new Label("descriptionLabel", "Confirmar prestamo de: "));
        form.add(new Label("bookNameLabel", this.book.getTitle()));

        // Accept Button
        form.add(new Button("accept_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {
                // LoanConfirmationComponent.this.replaceWith(new
                // CannotLoanDueToMaxLoansReachedComponent(
                // LoanConfirmationComponent.this.getId(), aModel));

                LoanConfirmationComponent.this.getLoanService().newCommonLoan(LoanConfirmationComponent.this.book,
                        aModel);

                if (aModel.getUserAccount().getRole().equals(LibrarianRole.getInstance())) {
                    LoanConfirmationComponent.this.replaceWith(new MainLibrarianComponent(
                            LoanConfirmationComponent.this.getId()) {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public FeedbackPanel getFeedbackTarget() {
                            return LoanConfirmationComponent.this.getFeedbackTarget();
                        }

                        @Override
                        public Map<String, WrapperPanel> getReplaceableComponents() {
                            return LoanConfirmationComponent.this.getReplaceableComponents();
                        }

                    });
                } else {
                    LoanConfirmationComponent.this.replaceWith(new MainUserComponent(LoanConfirmationComponent.this
                            .getId()));
                }
                LoanConfirmationComponent.this.getFeedbackTarget().info("Libro alquilado!");
                LoanConfirmationComponent.this.getReplaceableComponents().get("centerPanel")
                        .replace(new MainSearchComponent(WrapperPanel.INTERNALID) {

                            private static final long serialVersionUID = 1L;

                            @Override
                            public Map<String, WrapperPanel> getReplaceableComponents() {
                                return LoanConfirmationComponent.this.getReplaceableComponents();
                            }

                            @Override
                            public FeedbackPanel getFeedbackTarget() {
                                return LoanConfirmationComponent.this.getFeedbackTarget();
                            }
                        });
            }

        });

        // Cancel Button
        form.add(new Button("cancel_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {
                // LoanConfirmationComponent.this.goBack(aModel);

                if (aModel.getUserAccount().getRole().equals(LibrarianRole.getInstance())) {
                    LoanConfirmationComponent.this.replaceWith(new MainLibrarianComponent(
                            LoanConfirmationComponent.this.getId()) {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public FeedbackPanel getFeedbackTarget() {
                            return LoanConfirmationComponent.this.getFeedbackTarget();
                        }

                        @Override
                        public Map<String, WrapperPanel> getReplaceableComponents() {
                            return LoanConfirmationComponent.this.getReplaceableComponents();
                        }

                    });
                } else {
                    LoanConfirmationComponent.this.replaceWith(new MainUserComponent(LoanConfirmationComponent.this
                            .getId()));
                }
            }

        });

        this.add(form);

    }

    public void setBook(final BookInfoDTO book) {
        this.book = book;
    }

    public BookInfoDTO getBook() {
        return this.book;
    }

    public void setLoanService(final LoanService loanService) {
        this.loanService = loanService;
    }

    public LoanService getLoanService() {
        return this.loanService;
    }

}
